<?php

require_once __DIR__."/../constants.php";
require_once VENDOR_DIR."autoload.php";

use Predis\Client;

$redis = new Client('tcp://127.0.0.1:6379');

const HASH_CONTENT_LIST = "hash:content:list";
const HASH_DESCRIPTION = "hash:description";
const HASH_CATEGORY_LIST = "hash:category:list";

//重新构建命令描述和命令分类
$redis->del([HASH_DESCRIPTION,HASH_CATEGORY_LIST]);

$categoryList = [];
for ($cursor = 0; $cursor >=0; ) {
    $result = $redis->hscan(HASH_CONTENT_LIST, $cursor, ['MATCH'=>'*','COUNT'=>10]);
    foreach ($result[1] as $cmd => $content) {
        echo $cmd."\n";
        $contentArray = json_decode($content, true);
        $categoryList[$contentArray['category']][] = $cmd;
        $redis->hset(HASH_DESCRIPTION, $cmd."___".$contentArray['description'],$cmd);
    }
    $cursor = $result[0];
    if($cursor == 0) {
        break;
    }
}
foreach ($categoryList as $category => $cmdList) {
    $redis->hset(HASH_CATEGORY_LIST, $category, json_encode($cmdList));
}

